﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ISafetyDevice
    {
        public DataSet SafetyDevice_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet SafetyDevice_GetList(out int ItemCount, int BranchID, int DepartmentID, string keyword, string Type, string orderBy, string orderDirection, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@OrderBy", orderBy);
            dbCmd.Parameters.AddWithValue("@OrderDirection", orderDirection);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@keyword", keyword);
            dbCmd.Parameters.AddWithValue("@Type", Type);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }


        public DataSet SafetyDevice_GetWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SafetyDevice_EmployeeID", EmployeeID);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public SafetyDeviceInfo SafetyDevice_GetInfo(int SafetyDeviceID)
        {
            SafetyDeviceInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SafetyDevice_ID", SafetyDeviceID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new SafetyDeviceInfo();
                    retVal.ID = Convert.ToInt32(dr["SafetyDevice_ID"]);
                    retVal.EmployeeID = dr["SafetyDevice_EmployeeID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["SafetyDevice_EmployeeID"]);
                    retVal.BranchID = dr["SafetyDevice_BranchID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["SafetyDevice_BranchID"]);
                    retVal.DepartmentID = dr["SafetyDevice_DepartmentID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["SafetyDevice_DepartmentID"]);
                    retVal.Date = dr["SafetyDevice_Date"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["SafetyDevice_Date"]);
                    retVal.Name = dr["SafetyDevice_Name"].ToString();
                    retVal.Type = dr["SafetyDevice_Type"].ToString();
                    retVal.No = dr["SafetyDevice_No"] == DBNull.Value ? 0 : Convert.ToInt32(dr["SafetyDevice_No"]);
                    retVal.Status = dr["SafetyDevice_Status"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int SafetyDevice_Insert(SafetyDeviceInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            if (eInfo.EmployeeID > 0)
                dbCmd.Parameters.AddWithValue("@SafetyDevice_EmployeeID", eInfo.EmployeeID);
            else
                dbCmd.Parameters.AddWithValue("@SafetyDevice_EmployeeID", DBNull.Value);
            if (eInfo.BranchID > 0)
                dbCmd.Parameters.AddWithValue("@SafetyDevice_BranchID", eInfo.BranchID);
            else
                dbCmd.Parameters.AddWithValue("@SafetyDevice_BranchID", DBNull.Value);
            if (eInfo.DepartmentID > 0)
                dbCmd.Parameters.AddWithValue("@SafetyDevice_DepartmentID", eInfo.DepartmentID);
            else
                dbCmd.Parameters.AddWithValue("@SafetyDevice_DepartmentID", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_No", eInfo.No);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Status", eInfo.Status);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool SafetyDevice_Update(SafetyDeviceInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("SafetyDevice_ID", eInfo.ID);
            if (eInfo.EmployeeID > 0)
                dbCmd.Parameters.AddWithValue("@SafetyDevice_EmployeeID", eInfo.EmployeeID);
            else
                dbCmd.Parameters.AddWithValue("@SafetyDevice_EmployeeID", DBNull.Value);
            if (eInfo.BranchID > 0)
                dbCmd.Parameters.AddWithValue("@SafetyDevice_BranchID", eInfo.BranchID);
            else
                dbCmd.Parameters.AddWithValue("@SafetyDevice_BranchID", DBNull.Value);
            if (eInfo.DepartmentID > 0)
                dbCmd.Parameters.AddWithValue("@SafetyDevice_DepartmentID", eInfo.DepartmentID);
            else
                dbCmd.Parameters.AddWithValue("@SafetyDevice_DepartmentID", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_No", eInfo.No);
            dbCmd.Parameters.AddWithValue("@SafetyDevice_Status", eInfo.Status);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool SafetyDevice_UpdateEmployee(int EmployeeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_UpdateEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool SafetyDevice_Delete(int SafetyDeviceID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SafetyDevice_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SafetyDevice_ID", SafetyDeviceID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

